//
//  SEChatViewController+Keyboard.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import Foundation
import UIKit

// MARK: - 所有的键盘动画都是控制 chatActionBar 的 bottomConstraint

extension SEChatViewController {
    /// 键盘控制
    func setupKeyboardControl() {
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyboardWillShow),
                                               name: UIResponder.keyboardWillShowNotification,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyboardWillHide),
                                               name: UIResponder.keyboardWillHideNotification,
                                               object: nil)
    }
    
    @objc private func keyboardWillShow(_ noti: Notification) {
        keyboardControl(noti, isShowing: true)
    }
    
    @objc private func keyboardWillHide(_ noti: Notification) {
        keyboardControl(noti, isShowing: false)
    }
    
    /// 控制键盘事件
    /// - Parameters:
    ///   - noti:      NSNotification 对象
    ///   - isShowing: 是否显示键盘
    private func keyboardControl(_ noti: Notification, isShowing: Bool) {
        // 处理 Default, Text 的键盘属性
        let userInfo = noti.userInfo!
        let keyboardRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
        let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt32
        
        let convertedFrame = view.convert(keyboardRect!, from: nil)
        
        let heightOffset = view.bounds.size.height - convertedFrame.origin.y
        let options = UIView.AnimationOptions(rawValue: UInt(curve!) << 16 | UIView.AnimationOptions.beginFromCurrentState.rawValue)
        let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
        
        actionBarPaddingBottomConstranit?.update(offset: -heightOffset - (isShowing ? 0 : UIScreen.bottomSafeHeight))
        msgTableView.stopScrolling()
        
        UIView.animate(withDuration: duration, delay: 0, options: options) {
            self.view.layoutIfNeeded()
            if isShowing {
                self.msgTableView.scrollBottomToLastRow()
            }
        }
    }
    
    /// 隐藏自定义键盘，当唤醒的自定义键盘时候，这时候点击切换录音 button。需要隐藏掉
    func hideCustomKeyboard() {
        let heightOffset: CGFloat = 0
        actionBarPaddingBottomConstranit?.update(offset: -heightOffset - UIScreen.bottomSafeHeight)
        
        chatMoreView.snp.updateConstraints { (make) in
            make.top.equalTo(self.chatActionBarView.snp.bottom).offset(UIScreen.bottomSafeHeight)
        }
        
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear) {
            self.view.layoutIfNeeded()
        }
    }
    
    /// 隐藏所有键盘,
    /// 使用场景：
    ///   1.点击 UITableView 使用
    ///   2.开始滚动 UITableView 使用
    @objc func hideAllKeyboard() {
        hideCustomKeyboard()
        chatActionBarView.resignKeyboard()
    }
}
